package com.example.demo02.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.demo02.constants.RedisConstants;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * redis自定义工具类--全部
 */
@Component
public class MyRedisUtils {

    private static final String PROJECTNAME_SYSPARAM = RedisConstants.PROJECTNAME + RedisConstants.SYSPARAM;

    private static final String PROJECTNAME_SYSPARAMS = RedisConstants.PROJECTNAME + RedisConstants.SYSPARAMS;

    private static final String PROJECTNAME_THIRDACCOUNT = RedisConstants.PROJECTNAME + RedisConstants.THIRDACCOUNT;

    private static final String THIRDACCOUNT_ACCESSTOKEN = RedisConstants.THIRDACCOUNT_ACCESSTOKEN;

    private static final String THIRDACCOUNT_SIGNKEY = RedisConstants.THIRDACCOUNT_SIGNKEY;

    private static final String THIRDACCOUNT_PORTPWD = RedisConstants.THIRDACCOUNT_PORTPWD;

    private static final String THIRDACCOUNT_EXPIRESIN = RedisConstants.THIRDACCOUNT_EXPIRESIN;

    private static final String PROJECTNAME_THIRDACCOUNTPARK = RedisConstants.PROJECTNAME + RedisConstants.THIRDACCOUNTPARK;

    private static final String PROJECTNAME_CAROWNER = RedisConstants.PROJECTNAME + RedisConstants.CAROWNER;

    private static final String PROJECTNAME_GATE = RedisConstants.PROJECTNAME + RedisConstants.GATE;

    private static final String PROJECTNAME_REGION = RedisConstants.PROJECTNAME + RedisConstants.REGION;

    private static final String PROJECTNAME_PARKPOTCONFIG = RedisConstants.PROJECTNAME + RedisConstants.PARKPOTCONFIG;

    private static final String PROJECTNAME_CAR = RedisConstants.PROJECTNAME + RedisConstants.CAR;

    private static final String PROJECTNAME_SPECIALCAR = RedisConstants.PROJECTNAME + RedisConstants.SPECIALCAR;

    private static final String PROJECTNAME_PERMITRULE = RedisConstants.PROJECTNAME + RedisConstants.PERMITRULE;

    private static final String PROJECTNAME_BOX = RedisConstants.PROJECTNAME + RedisConstants.BOX;

    private static final String PROJECTNAME_PARKPOT = RedisConstants.PROJECTNAME + RedisConstants.PARKPOT;

    private static final String PROJECTNAME_UTOKEN_ = RedisConstants.PROJECTNAME + RedisConstants.ACCOUNT + RedisConstants.UTOKEN_;

    private static final String PROJECTNAME_ACCOUNTID_ = RedisConstants.PROJECTNAME + RedisConstants.ACCOUNT + RedisConstants.ACCOUNTID_;

    private static final String PROJECTNAME_OPTUNIT = RedisConstants.PROJECTNAME + RedisConstants.OPTUNIT;

    //节假日配置
    private static final String PROJECTNAME_CALENDAR = RedisConstants.PROJECTNAME + RedisConstants.CALENDAR;
    //语音模板配置
    private static final String PROJECTNAME_PARKPOTDISPLAY = RedisConstants.PROJECTNAME + RedisConstants.PARKPOTDISPLAY;
    //车牌包月
    private static final String PROJECTNAME_RENTCAR=RedisConstants.PROJECTNAME + RedisConstants.RENTCAR;
    //上级上送平台
    private static final String PROJECTNAME_CLOUDPLAT=RedisConstants.PROJECTNAME + RedisConstants.CLOUDPLAT;
    //ETC
    private static final String PROJECTNAME_ETC=RedisConstants.PROJECTNAME + RedisConstants.ETC;

    //过期时间变量
    private static final long TEN_MIN = RedisConstants.TEN_MIN;
    private static final long ONE_DAY = RedisConstants.ONE_DAY;
    private static final long SEVEN_DAY = RedisConstants.SEVEN_DAY;

    @Resource
    private RedisTemplate redisTemplate;

    //------数据字典、系统参数、第三方账号-------

    //------数据字典------

    /**
     * 加入数据字典
     *
     * @param parameterCode 父参数code
     * @param itemmemos     子参数code-value map集合
     */
    public void addParameter(String parameterCode, Map<String, String> itemmemos) {
        if (itemmemos == null) {
            itemmemos = new HashMap<>();
        }
        this.redisTemplate.opsForHash().put(PROJECTNAME_SYSPARAM, parameterCode, JSONObject.toJSON(itemmemos).toString());
    }

    /**
     * 获取子参数值
     *
     * @param parameterCode 父参数code
     * @param itemcode      子参数code
     */
    public String getParameterValue(String parameterCode, String itemcode) {
        try {
            Map items = JSONObject.parseObject(this.redisTemplate.opsForHash().get(PROJECTNAME_SYSPARAM, parameterCode).toString(), HashMap.class);
            return (String) items.get(itemcode);
        } catch (Exception e) {
            return "";
        }
    }

}
